var gBatSearchClick = false;
var gBatDeckAltClick = false;

function ocsInit(event)
{
	window.addEventListener("keyup", WindowKeyPress, true);
	document.getElementById("threadTree").addEventListener("mousedown", ocsThreadPaneSelectionMousedown, true);
}

function BatSearchClicked(e)
{
	if ( !e.altKey ) return;

	gBatSearchClick = true;

	var row = {};
	var col = {};
	var childElt = {};
	e.currentTarget.treeBoxObject.getCellAt(e.clientX, e.clientY, row, col, childElt);

	var sCellText = e.currentTarget.view.getCellText(row.value, col.value);
	var aMenuItem = {};
	var searchMode;

	if ( col.value.id == "subjectCol" )
		searchMode = 0;
	else if ( col.value.id == "senderCol" )
		searchMode = 1;
	else if ( col.value.id == "recipientCol" )
		searchMode = 5;
	else 
		return;
	
	document.getElementById("searchInput").value = sCellText;
	document.getElementById("searchInput").clearButtonHidden = false;

	aMenuItem.value = searchMode; //sender
	gSearchInput.showingSearchCriteria = false;
	changeQuickSearchMode(aMenuItem);
	onSearchInput(true);
	e.preventDefault();
	e.stopPropagation();
}

function SearchKeyPress(event)
{
	if (gSearchInput.showingSearchCriteria)
		gSearchInput.showingSearchCriteria = false;

	// 27 == esc
	if (event && event.keyCode == 27)
		onClearSearch();
	// 13 == return
	if (event && event.keyCode == 13)
		onSearchInput(true);
}

function WindowKeyPress(e)
{
	dump("keyup, altKey = " + e.altKey + ", charCode = " + e.charCode + ", keyCode = " + e.keyCode + "\n");
	if ( gBatSearchClick && e.altKey == false && e.charCode == 0 && e.keyCode == 18 )
	{
		gBatSearchClick = false;
		dump("Stoped propagation\n");
		e.stopPropagation();
		return;
	}
	if ( e.keyCode == 27 )
	{
		onClearSearch();
	}
}

function ocsThreadPaneSelectionChanged(event)
{
	if (!gBatDeckAltClick) 
		ThreadPaneSelectionChanged(event);
	else
		gBatDeckAltClick = false;
}

function ocsThreadPaneSelectionMousedown(event)
{
	if (event.altKey) gBatDeckAltClick = true;
}